<template>
    <n-layout has-sider style="height: 83vh">
        <n-layout-sider bordered>
            <n-layout>
                <n-layout-header style="height: 4vh">
                    <n-input v-model:value="pattern" placeholder="搜索"/>
                </n-layout-header>
                <n-layout-content style="height: 79vh" :native-scrollbar="false">
                    <n-tree
                        :show-irrelevant-nodes="false"
                        :pattern="pattern"
                        block-line
                        :data="data"
                        :default-expand-all="true"
                        label-field="name"
                        selectable
                        :render-prefix="renderPrefix"
                        @update:selected-keys="selected"
                    />
                </n-layout-content>
            </n-layout>
        </n-layout-sider>
        <n-layout bordered content-style="padding: 10px;">
            <slot name="fileShow"/>
        </n-layout>
    </n-layout>
</template>

<script setup>
import {uuid} from "@/hooks/uuid";
import {NIcon} from "naive-ui";
import {Folder16Regular, DocumentPdf32Regular, DocumentTextExtract24Regular} from "@vicons/fluent"
import {CopyFile} from "@vicons/carbon"
const emit = defineEmits(['onSelected'])
const props = defineProps(["directory"])
const data = computed(()=>{
    return props.directory.map(s => {
        s.name = s.category
        s.key = uuid()
        s.children.map(l => {
            l.key = uuid()
            return l
        })
        return s
    })
})

// 搜索
const pattern = ref('')

// 前缀图标
function renderPrefix({option}) {
    if (option.category) {
        return h(NIcon, {size: "25"}, {
            default: () => h(Folder16Regular)
        });
    }
    if (option.type === 'file') {
        switch (option.file_type) {
            case "pdf":
                return h(NIcon, {size: "25"}, {
                    default: () => h(DocumentPdf32Regular)
                });
            case "docx":
                return h(NIcon, {size: "25"}, {
                    default: () => h(DocumentTextExtract24Regular)
                });
        }
        return h(NIcon, {size: "25"}, {
            default: () => h(CopyFile)
        });
    }
}

function selected(keys, option, meta) {
    console.log(keys)
    console.log(meta)
    emit('onSelected', {option:option[0]})
}
</script>

<style scoped lang="less">

</style>